// subgraph isomorphism algorithm: boost_1.55 implementation
// Jeff Eaton 2014

#include "main.h"

int main(int argc, char** argv) {
	std::string fileName = "";
	std::string path = "";
	std::stringstream ss;
	
	if(argc == 1) {
		std::cout << "Enter name of graph definition text file (test): ";
		std::cin >> fileName;

		ss << fileName << ".txt";
		ss >> path;
		ss.str("");
		ss.clear();

	}
	else {
		std::cout << "Using graph definition file " << argv[1] << "." << std::endl;
		fileName = std::string(argv[1]);
		fileName = fileName.substr(0, fileName.find("."));
		path = argv[1];
	}

	std::ifstream fileIn(path.c_str(), std::ifstream::in);


	std::vector<std::pair<int, std::string> > mainGraphVertices;

	// build a vector of pairs associating a vertex name with a number
	std::string tmp;
	bool exists;
	while(fileIn.good()) {
		exists = false;
		fileIn >> tmp;
		for(unsigned int i = 0; i < mainGraphVertices.size(); i++) {
			if(mainGraphVertices[i].second == tmp)
				exists = true;
		}
		if(!exists)
			mainGraphVertices.push_back(std::pair<int, std::string>(mainGraphVertices.size(),tmp));
	}

	Graph mainGraph(mainGraphVertices.size());

	fileIn.clear();
	fileIn.seekg(0);

	int v1;
	int v2;
	std::string tmp2;
	while(fileIn.good()) {
		fileIn >> tmp;
		if(!fileIn.good())
			break;

		fileIn >> tmp2;
		for(unsigned int i = 0; i < mainGraphVertices.size(); i++) {
			if(mainGraphVertices[i].second == tmp) {
				v1 = mainGraphVertices[i].first;
			}
			if(mainGraphVertices[i].second == tmp2) {
				v2 = mainGraphVertices[i].first;
			}
		}
		if(!edge(v1, v2, mainGraph).second) // check for duplicates
			add_edge(v1, v2, mainGraph);
	}

	fileIn.close();
	// the main graph should be built at this point
	std::cout << "\nInput Graph Loaded:" << std::endl;
	print_graph(mainGraph);
	std::cout << std::endl;


	// load each subgraph 0-29 and build its signature
	
	ss.str("");
	ss.clear();
	ss << fileName << "_signature.txt";
	std::string resultFileName = ss.str();
	std::ofstream signatureOut(resultFileName.c_str(), std::ofstream::out);
	int numMappings;
	
	for(int i = 0; i < 30; i++) {
		Edges e;
		Graph subgraph = loadSubgraph(i);

		std::cout << "Processing Graphlet " << i << "/29...   ";
		vf2_callback callback(subgraph, mainGraph, e);
		vf2_subgraph_mono(subgraph, mainGraph, callback);

		ss.str("");
		ss.clear();
		ss << fileName << "_g" << i << ".txt";
		path = ss.str();

		numMappings = e.printCombinations(mainGraphVertices, path);
		signatureOut << numMappings << ", ";
		std::cout << "done" << std::endl;
	}
	signatureOut.close();

	std::cout << "Done!\n" << std::endl;

	return EXIT_SUCCESS;
}

Graph loadSubgraph(int subgraphID) {
	std::string path = "";
	std::stringstream ss;

	Graph tmp;
	int v1;
	int v2;

	if(subgraphID < 0 || subgraphID > 29) {
		std::cout << "Invalid subgraph ID - defaulting to g0" << std::endl;
		path = "graphlets/2/g0.txt";
	}
	else if(subgraphID == 0) {
		path = "graphlets/2/g0.txt";
	}
	else if(subgraphID <= 2) {
		ss << "graphlets/3/g" << subgraphID << ".txt";
		ss >> path;
	}
	else if(subgraphID <= 8) {
		ss << "graphlets/4/g" << subgraphID << ".txt";
		ss >> path;
	}
	else if(subgraphID <= 29) {
		ss << "graphlets/5/g" << subgraphID << ".txt";
		ss >> path;
	}
	
	std::ifstream fileIn(path.c_str(), std::ifstream::in);

	while(fileIn.good()) {
		fileIn >> v1;
		fileIn >> v2;
		add_edge(v1, v2, tmp);
	}
	fileIn.close();

	return tmp;
}
